package com.fenghuajueli.idiomppp.ui.idiomxiao;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.blankj.utilcode.util.LogUtils;
import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.adapter.IdiomXiaoGameAdapter;
import com.fenghuajueli.idiomppp.database.IdiomDaoUtils;
import com.fenghuajueli.idiomppp.databinding.ActivityIdiomXiaoGameBinding;
import com.fenghuajueli.idiomppp.entity.IdiomShiYiEntity;
import com.fenghuajueli.idiomppp.entity.IdiomXiaoDbEntity;
import com.fenghuajueli.idiomppp.entity.IdiomXiaoGameEntity;
import com.fenghuajueli.idiomppp.utils.MmkvKey;
import com.fenghuajueli.idiomppp.utils.RuntimeDataUtils;
import com.fenghuajueli.libbasecoreui.listener.OnBaseClick;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;
import com.fenghuajueli.libbasecoreui.mvp.BaseActivity;
import com.fenghuajueli.libbasecoreui.utils.ImageLoadManager;
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils;
import com.fenghuajueli.libbasecoreui.utils.MyStatusBarUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * 成语消消消游戏界面
 */
public class IdiomXiaoGameActivity extends BaseActivity {
    LinearLayout idiomShiYiContainer;
    TextView idiomName;
    TextView idiomShiyi;
    RelativeLayout containerView;
    GridView gridView;
    TextView tvTitle;
    private List<IdiomXiaoGameEntity> idiomXiaoGameEntityList;
    private IdiomXiaoDbEntity idiomXiaoDbEntity;
    private List<String> idiomList;
    private List<String> idiomRepeatList;
    private List<IdiomXiaoGameEntity> allWordList;
    private IdiomXiaoGameAdapter idiomXiaoGameAdapter;
    CompositeDisposable compositeDisposable = new CompositeDisposable();
    private AlertDialog.Builder builder;

    private ActivityIdiomXiaoGameBinding binding;

    private void initview() {
        idiomShiYiContainer = binding.idiomShiYiContainer;
        idiomName = binding.idiomName;
        idiomShiyi = binding.idiomShiyi;
        containerView = binding.containerView;
        gridView = binding.gridView;
        tvTitle = binding.tvTitle;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityIdiomXiaoGameBinding.inflate(getLayoutInflater());
        MyStatusBarUtils.setStatusBar(this,"#FFB856");
        setContentView(binding.getRoot());
        initview();
        ImageLoadManager.loadForViewGroup(containerView, R.mipmap.aa_szxl_bj);
        idiomXiaoDbEntity = RuntimeDataUtils.idiomXiaoDbEntity;
        tvTitle.setText("成语消消");
        if (idiomXiaoDbEntity != null) {
            init();
        } else {
            finish();
        }
    }


    private void init() {
        idiomXiaoGameEntityList = new ArrayList<>();
        allWordList = new ArrayList<>();
        idiomList = new ArrayList<>();
        idiomRepeatList = new ArrayList<>();
        idiomList.addAll(Arrays.asList(idiomXiaoDbEntity.getIdioms().split(",")));
        if (idiomXiaoDbEntity.getRepeat_idioms() != null && idiomXiaoDbEntity.getRepeat_idioms().length() > 0) {
            idiomRepeatList.addAll(Arrays.asList(idiomXiaoDbEntity.getRepeat_idioms().split(",")));
        }
        for (int i = 0; i < idiomList.size(); i++) {
            LogUtils.d("没有重复的成语：" + idiomList.get(i));
        }
        for (int i = 0; i < idiomRepeatList.size(); i++) {
            LogUtils.d("重复的成语：" + idiomRepeatList.get(i));
        }
        for (int i = 0; i < idiomList.size(); i++) {
            char[] data = idiomList.get(i).toCharArray();
            for (int j = 0; j < data.length; j++) {
                IdiomXiaoGameEntity idiomXiaoGameEntity = new IdiomXiaoGameEntity(String.valueOf(data[j]), j);
                allWordList.add(idiomXiaoGameEntity);
            }
        }
        Collections.shuffle(allWordList);
        idiomXiaoGameAdapter = new IdiomXiaoGameAdapter(this, allWordList);
        idiomXiaoGameAdapter.setIdiomList(idiomList);
        idiomXiaoGameAdapter.setIdiomRepeatList(idiomRepeatList);
        idiomXiaoGameAdapter.setOnBaseClick(new OnListClickListener<String>() {
            @Override
            public void itemClick(int position, String data) {
                try {
                    IdiomShiYiEntity idiomShiYiEntity = IdiomDaoUtils.getIdiomShiYiManager().getOneData(data);
                    if (idiomShiYiEntity != null) {
                        idiomName.setText(idiomShiYiEntity.getIdiom() + "(" + idiomShiYiEntity.getPronounce() + ")");
                        idiomShiyi.setText(idiomShiYiEntity.getExplain());
                        idiomShiYiContainer.setVisibility(View.VISIBLE);
                        delayHide();
                    }
                } catch (Exception e) {

                }
            }
        });
        idiomXiaoGameAdapter.setOnBaseAllRightClick(new OnBaseClick<Integer>() {
            @Override
            public void onClick(Integer integer) {
                showPassDialog();
            }
        });
        gridView.setAdapter(idiomXiaoGameAdapter);
    }

    /**
     * 定时隐藏
     */
    private void delayHide() {
        Observable.timer(3, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
            }

            @Override
            public void onNext(Long aLong) {
                idiomShiYiContainer.setVisibility(View.GONE);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        compositeDisposable.dispose();
        compositeDisposable.clear();
    }

    /**
     * 显示通关之后的提示，进入下一关
     */
    private void showPassDialog() {
        if (MmkvUtils.mmkv.decodeInt(MmkvKey.IdiomXiaoPass.name(), 0) <= idiomXiaoDbEntity.getId().intValue()) {
            MmkvUtils.mmkv.encode(MmkvKey.IdiomXiaoPass.name(), idiomXiaoDbEntity.getId().intValue() + 1);
        }
        builder = new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("恭喜通过第" + (idiomXiaoDbEntity.getId()) + "关")
                .setNegativeButton("下一关", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (idiomXiaoDbEntity.getId() + 1 < RuntimeDataUtils.idiomXiaoDbEntityList.size()) {
                            idiomXiaoDbEntity = RuntimeDataUtils.idiomXiaoDbEntityList.get(idiomXiaoDbEntity.getId().intValue());
                            RuntimeDataUtils.idiomXiaoDbEntity = idiomXiaoDbEntity;
                            init();
                        }
                    }
                }).setNeutralButton("返回", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                });
        builder.create().show();
    }
}
